<template>
  <el-dialog
    title="查看PDF"
    :visible.sync="dialogVisible"
    width="60%">
    <p class="arrow">
      <el-button @click="changePdfPage(0)" class="turn" :class="{grey: currentPage==1}">上一页</el-button>
      {{currentPage}} / {{pageCount}}
      <el-button @click="changePdfPage(1)" class="turn" :class="{grey: currentPage==pageCount}">下一页</el-button>
    </p>
    <p>
      <el-button @click="print">
        打印
      </el-button>
    </p>
    <pdf
      src="static/pdf/2022前端工程师面试题手册.pdf"
      :page="currentPage"
      ref="mypdf"
      @num-pages="pageCount=$event"
      @page-loaded="currentPage=$event"
      @loaded="loadPdfHandler"
      style="width: 800px ;"
    ></pdf>
<!--    <pdf-->
<!--      v-for="i in numPages"-->
<!--      :key="i"-->
<!--      :src="src"-->
<!--      :page="i"-->
<!--      style="display: inline-block; width: 25%"-->
<!--    ></pdf>-->
    <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
  </span>
  </el-dialog>
</template>

<script>
import pdf from "vue-pdf"
// var pdfsource = pdf.createPdfSource("2022前端工程师面试题手册.pdf")
export default {
  components: {
    pdf
  },
  data () {
    return {
      dialogVisible: false,
      currentPage: 0,
      pageCount: 0,
      fileType: "pdf"
      // src: pdfsource,
      // numPages: undefined
    }
  },
  methods: {
    // 改变PDF页码,val传过来区分上一页下一页的值,0上一页,1下一页
    changePdfPage (val) {
      // console.log(val)
      if (val === 0 && this.currentPage > 1) {
        this.currentPage--
        // console.log(this.currentPage)
      }
      if (val === 1 && this.currentPage < this.pageCount) {
        this.currentPage++
        // console.log(this.currentPage)
      }
    },
    // pdf加载时
    loadPdfHandler (e) {
      this.currentPage = 1 // 加载的时候先加载第一页
    },
    print () {
      this.$refs.mypdf.print()
    }
  }
  // mounted () {
  //   this.src.promise.then(pdf => {
  //     this.numPages = pdf.numPages
  //   })
  // }
}

</script>

<style scoped>

</style>
